﻿using System;
using System.Xml.Linq;
using BattleFury.Lib.Extensions;
using BattleFury.Lib.Input;
using Microsoft.Xna.Framework;

namespace BattleFury.Lib.Controls
{
	public class Button
		: Control
	{
        //private string _state = "Button";
		private bool _isHighlighted;
		private bool _isClicked;

		public string Text { get; set; }
		public Color ForegroundColor { get; set; }
		public Action OnClick { get; set; }

		#region Serialization

		public override void Deserialize(XElement element, object callingObject)
		{
			base.Deserialize(element, callingObject);

			ForegroundColor = element.GetColor("ForegroundColor", Color.Black);
			Text = element.GetString("Text");

			var onClick = element.GetString("OnClick", string.Empty);
			if (!string.IsNullOrEmpty(onClick) && callingObject != null)
			{
				var split = onClick.Split('.');
				var methodName = split[split.Length - 1];
				var callingType = callingObject.GetType();
				var method = callingType.GetMethod(methodName);
				OnClick = () => method.Invoke(callingObject, new object[] { });
			}
		}

		#endregion
		
		public override void Update(GameTime gameTime)
		{
			_isHighlighted = false;
			_isClicked = false;

			if (Input.Mouse.CurrentBounds.Intersects(Bounds))
			{
				_isHighlighted = true;
				if (Input.Mouse.IsMouseButtonHeld(MouseButtons.Left))
				{
					_isClicked = true;
				}
				if (Input.Mouse.IsMouseButtonPressed(MouseButtons.Left))
				{
					if (OnClick != null) OnClick();
				}
			}
		}

		public override void Draw(GameTime gameTime)
		{
			var prefix = "Button";
			if (_isHighlighted) prefix = "ButtonHighlighted";
			if (_isClicked) prefix = "ButtonClicked";
			if (_isHighlighted && _isClicked) prefix = "ButtonHighlightedClicked";

			var leftPartName = prefix + "Left";
			var leftPart = Skin[leftPartName];
			var leftBounds = new Rectangle((int)Position.X, (int)Position.Y, leftPart.Width, (int)Size.Y);
			SpriteBatch.Draw(Texture, leftBounds, leftPart.SourceBounds, Tint);

			var centerWidth = Size.X - 64;
			var xPos = 32;
			if (centerWidth == 0) xPos = 0;
			for (var x = 0; x < centerWidth / 32; x++)
			{
				var xInc = x;
				if (centerWidth == 0) xInc = (x + 1);
				xPos += xInc * 32;
				var centerPartName = prefix + "Center";
				var centerPart = Skin[centerPartName];
				var centerBounds = new Rectangle((int)Position.X + xPos, (int)Position.Y, centerPart.Width, (int)Size.Y);
				SpriteBatch.Draw(Texture, centerBounds, centerPart.SourceBounds, Tint);
			}

			var rightPartName = prefix + "Right";
			var rightPart = Skin[rightPartName];
			var rightBounds = new Rectangle((int)Position.X + xPos + 32, (int)Position.Y, rightPart.Width, (int)Size.Y);
			SpriteBatch.Draw(Texture, rightBounds, rightPart.SourceBounds, Tint);

			if (Font != null)
			{
				var textSize = Font.MeasureString(Text);
				var textPosition = (Size / 2) - (textSize / 2);
				var offset = _isClicked ? Vector2.One : Vector2.Zero;
				SpriteBatch.DrawString(Font, Text, Position + textPosition + Vector2.One + offset, Color.Black.WithOpacity(0.8f));
				SpriteBatch.DrawString(Font, Text, Position + textPosition + offset, ForegroundColor);
			}
		}
	}
}